#include "teachmodel.h"

#include "robotlist.h"

TeachModel::TeachModel(QObject *parent)
    : QAbstractListModel{parent}
{
}

void TeachModel::resetData()
{
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
    {
        emit beginResetModel();
        teachList=DatabaseManager::getInstance()->getTeach(pCurRobot->nID());
        emit endResetModel();
    }
}

QString TeachModel::getTeachJson(int nIndex)
{
    if(nIndex<teachList.size())
        return  teachList[nIndex].teachJson;
    return "";
}

QString TeachModel::teachDataToJson(QVector<TeachData> teachData)
{
    //拼接json字符串
    // QVector<JOINT> vJoint = getJointBody();
    QJsonObject jsonObj;
    for (int i=0;i<teachData.size();i++) {
        // 构建头部关节对象
        QJsonArray tempArray;
        for(int j=0;j<teachData[i].jointData.size();j++)
        {
            tempArray.append(teachData[i].jointData[j]);
        }

        tempArray.append(teachData[i].execTime);
        tempArray.append(teachData[i].waitTime);

        QString key="node"+QString::number(i+1);
        jsonObj[key]=tempArray;
    }
    QJsonDocument jsonDoc(jsonObj);
    return jsonDoc.toJson(QJsonDocument::Compact);
}

int TeachModel::getCountTime(int nIndex)
{
    int count=0;
    if(nIndex<teachList.size())
    {
        //不计算第一个点的时间和最后一个点的保持时间
        for(int i = 1; i < teachList[nIndex].vTeachData.size(); ++i)
        {
            count+=teachList[nIndex].vTeachData[i].execTime;
            if(i != teachList[nIndex].vTeachData.size() - 1)
                count+=teachList[nIndex].vTeachData[i].waitTime;
        }
    }
    return count;
}

QVector<TeachData> TeachModel::getTeachData(int nIndex)
{
    QVector<TeachData> teach;
    if(nIndex<teachList.size())
        teach= teachList[nIndex].vTeachData;
    return teach;
}

int TeachModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return teachList.count();
}

QVariant TeachModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()||teachList.size()==0 || teachList.size()<=index.row())
        return QVariant();
    if (role == NameRole)
        return teachList[index.row()].teachName;
    return QVariant();
}

bool TeachModel::insertTeach(const QString teachName, QVector<TeachData> vTeachData, QString &strErr)
{

    TeachInfo info;
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
        info.nRobotID = pCurRobot->nID();
    info.teachName = teachName;
    info.teachJson = teachDataToJson(vTeachData);
    info.vTeachData = vTeachData;
    bool nRet = DatabaseManager::getInstance()->addTeach(info,strErr);
    beginInsertRows(QModelIndex(),teachList.size(),teachList.size());
    teachList.push_back(info);
    endInsertRows();
    return nRet;
}

bool TeachModel::deleteTeach(int nIndex)
{
    if(nIndex>=teachList.size())
        return false;
    QString strErr;
    bool nRet = DatabaseManager::getInstance()->deleteTeach(teachList[nIndex].teachName,strErr);
    beginRemoveRows(QModelIndex(),nIndex,nIndex);
    teachList.removeAt(nIndex);
    endRemoveRows();
    return nRet;
}

bool TeachModel::editTeach(int nIndex, const QString teachName, const QString teachOldName, QVector<TeachData> vTeachData, QString &strErr)
{
    TeachInfo info;
    info.teachName = teachName;
    info.teachJson = teachDataToJson(vTeachData);
    info.vTeachData = vTeachData;
    bool nRet = DatabaseManager::getInstance()->editTeach(info,teachOldName,strErr);

    for (int i=0; i<teachList.size();i++) {
        if(teachList[i].teachName==teachOldName)
            teachList[i]=info;
    }
    emit dataChanged(index(nIndex),index(nIndex));
    return nRet;
}

